package com.nuedu.corejava.ch13;
/**
 * 
 * 线程不同步实例；会造成数据共享不安全问题；
 * 
 * @author admin
 *
 */

public class HomeWorkTeacher2 {
	private int count = 0;

	public  void add(int ThreadId) {
		count++;
		System.out.println(ThreadId + "," + count);
	}

	public  void desc(int ThreadId) {
		count--;
		System.out.println(ThreadId + "," + count);
	}

	class AddClass implements Runnable {
		private int ThreadId = 0;

		public AddClass(int id) {

			this.ThreadId = id;
		}

		@Override
		public void run() {
			for (int i = 0; i < 50; i++) {

				add(ThreadId);

//				try {
//					Thread.sleep(100);
//				} catch (InterruptedException e) {
//					e.printStackTrace();
//				}
			}
		}

	}

	class DescClass implements Runnable {
		private int ThreadId = 0;

		public DescClass(int id) {

			this.ThreadId = id;
		}

		@Override
		public void run() {
			for (int i = 0; i < 50; i++) {

				desc(ThreadId);
//
//				try {
//					Thread.sleep(100);
//				} catch (InterruptedException e) {
//					e.printStackTrace();
//				}
			}
		}

	}

	public static void main(String[] args) {
		HomeWorkTeacher2 hTheacher1 = new HomeWorkTeacher2();
		AddClass addClass1 = hTheacher1.new AddClass(1);
		AddClass addClass2 = hTheacher1.new AddClass(2);
		DescClass descClass1 = hTheacher1.new DescClass(3);
		DescClass descClass2 = hTheacher1.new DescClass(4);

		new Thread(addClass1).start();
		new Thread(addClass2).start();
		new Thread(descClass1).start();
		new Thread(descClass2).start();

	}


}
